//
//  RickyRefreshGifView.m
//  Refresh
//
//  Created by weitayinqing on 15/9/24.
//  Copyright © 2015年 苏昌雄. All rights reserved.
//

#import "RickyRefreshGifView.h"

static CGFloat const gifPathLength = 200;// 整个动画路径长度
static NSString *const gifAnimationKey = @"gifAnimationKey";

@interface  RickyRefreshGifView()
@property(nonatomic,strong)UIImageView *gifImagView;
@property(nonatomic,strong)UILabel *titleLabel;
@end

@implementation RickyRefreshGifView
 - (id)initWithRefreshViewType:(RickyRefreshViewType)viewType
{
    self = [super initWithRefreshViewType:viewType];
    if (self) {
        
    }
    return self;
}

#pragma mark ---
#pragma mark ----setter/getter

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel *label = [[UILabel alloc]init];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:RickyHeaderTitleFontSize];
            label.textColor = [UIColor blackColor];
            label.textAlignment =1;
            label;
        });
        [self insertSubview:_titleLabel atIndex:0];
    }
    return _titleLabel;
}

 -(UIImageView *)gifImagView
{
    if (!_gifImagView) {
        _gifImagView = ({
            UIImageView *imageView = [[UIImageView alloc]init];
            imageView.animationDuration = RickyGifAnimationDuration;
            imageView;
        });
        [self addSubview:_gifImagView];
    }
    return _gifImagView;
}

- (void)setPlaceHolderImage:(UIImage *)placeHolderImage
{
    if (_placeHolderImage != placeHolderImage) {
        _placeHolderImage = placeHolderImage;
        self.gifImagView.image = _placeHolderImage;
    }
}

- (void)setTitle:(NSString *)title
{
    if (title.length == 0) {
        return;
    }
    _title = title;
    self.titleLabel.text = _title;
}

- (void)setAnimationImages:(NSArray *)animationImages
{
    if (animationImages.count >0) {
        _animationImages = animationImages;
        self.gifImagView.animationImages = _animationImages;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.frame = self.bounds;
    
   //整条动画路径在中间，gifImageView在路径侧边
    CGSize imageSize = self.placeHolderImage.size;
    CGFloat imagScale = 0.6;
    self.gifImagView.frame  = CGRectMake((self.frame.size.width -gifPathLength)/2, (self.frame.size.height -imageSize.height*imagScale)/2, imageSize.width*imagScale, imageSize.height*imagScale);
}

#pragma mark -- 
#pragma mark --重写父类属性

- (void)setRefreshState:(RickyRefreshState)refreshState
{
    [super setRefreshState:refreshState];
    if (refreshState == RickyRefreshStateRefreshing) {
        self.titleLabel.hidden = YES;
        [self.gifImagView startAnimating];
        CABasicAnimation *maskAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
        maskAnimation.byValue = @(gifPathLength);
        maskAnimation.repeatCount = INT_MAX;
        maskAnimation.duration = RickyGifMoveAnimationDuration;
        [self.gifImagView.layer addAnimation:maskAnimation forKey:gifAnimationKey];
    }
}

- (void)setCompleteType:(RickyRefreshCompleteType)completeType
{
    [super setCompleteType:completeType];
    
    __weak __typeof(self)weakSelf = self;
    [self setRefreshState:RickyRefreshStateNormal withCallBack:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf.gifImagView stopAnimating];
        strongSelf.titleLabel.hidden = NO;
        [strongSelf.gifImagView.layer removeAnimationForKey:gifAnimationKey];
    }];

}
@end
